'''
@Project ：my_python_project 
@File    ：test1.py
@IDE     ：PyCharm 
@Author  ：xionglinhui
@Date    ：2024/10/14 下午6:22 
'''
# 一、类的定义
class Student:
    '''打印学生信息'''
    # 1、变量的定义
    stu_school = 'old_boy'

    # 2、功能的定义
    def tell_stu_info(stu_obj):
        print('学生信息：名字：%s 年龄：%s 性别：%s'%(
                  stu_obj['stu_name'],
                  stu_obj['stu_age'],
                  stu_obj['stu_gender']
              ))
    def set_info(stu_obj,x,y,z):
        stu_obj['stu_name'] = x,
        stu_obj['stu_age'] = y,
        stu_obj['stu_gender'] = z

    print('==== 类 类 类 ====')


# print(Student.__dict__)
'''{
'__module__': '__main__', 
'stu_school': 'old_boy', 
'tell_stu_info': <function Student.tell_stu_info at 0x7f41adb7cd08>, 
'set_info': <function Student.set_info at 0x7f41adb7cd90>, 
'__dict__': <attribute '__dict__' of 'Student' objects>, 
'__weakref__': <attribute '__weakref__' of 'Student' objects>, stu_)
'__doc__': None}

'''

# 1、访问数据属性
# print(Student.stu_school)  # 输出：old_boy  ==> 等价于print(Student.__dict__['stu_school'])

# 2、访问函数属性
# print(Student.tell_stu_info) # 输出：<function Student.tell_stu_info at 0x7f11d6b55d08>
                            # 等价于 print(Student.__dict__['tell_stu_info'])

# Student.xx = 123
# print(Student.__dict__) # 打印结果中新增了 'xx': 123，说明可以创造属性


# 二：再调用类产生对象
stu1_obj = Student()
stu2_obj = Student()
stu3_obj = Student()
# print(stu1_obj.__dict__)   # 输出：{}
# print(stu2_obj.__dict__)   # 输出：{}
# print(stu3_obj.__dict__)   # 输出：{}

# 1、为对象定制自己独有的属性
# stu1_obj.stu_name = 'linhui'
# stu1_obj.stu_age = '19'
# stu1_obj.stu_gender = 'male'
# print(stu1_obj.__dict__)     # 创建的属性：{'stu_name': 'linhui', 'stu_age': '19', 'stu_gender': 'male'}
#
# stu2_obj.stu_name = 'dafeifei'
# stu2_obj.stu_age = '20'
# stu2_obj.stu_gender = 'male'
# print(stu2_obj.__dict__)     # 创建的属性：{'stu_name': 'dafeifei', 'stu_age': '20', 'stu_gender': 'male'}
#
# stu3_obj.stu_name = "xiong"
# stu3_obj.stu_age = '21'
# stu3_obj.stu_gender = 'male'
# print(stu3_obj.__dict__)

# 上述代码遗留两个问题
# 问题1：代码重复
# 问题2：属性的查找顺序
#
# 【解决问题1】
# 解决方案一：
def init(obj,x,y,z):
    obj.stu_name = x,
    obj.stu_age = y,
    obj.stu_gender = z

init(stu1_obj, 'linhui','19','male')
print(stu1_obj.__dict__)  # 创建stu1_obj的属性 {'stu_name': ('linhui',), 'stu_age': ('19',), 'stu_gender': 'male'}

# 解决方案二：
# 一：先定义类,见test2.py